package fwhmc.rt.registrar;

import fwhmc.rt.AbstractHttpRestTemplateManager;
import fwhmc.rt.IHttpRestTemplateManager;
import fwhmc.rt.properties.RestTemplateConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 类描述:  RestTemplate处理对象注入处理
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2021/4/22 0022 上午 9:39
 */
@Slf4j
@EnableConfigurationProperties(value = RestTemplateConfig.class)
public class RestTemplateManagerRegistrar {

    @Bean()
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateConfig restTemplateConfig) {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(restTemplateConfig.getConnectTimeOut());
        httpRequestFactory.setReadTimeout(restTemplateConfig.getReadTimeOut());
        httpRequestFactory.setConnectionRequestTimeout(restTemplateConfig.getConnectionRequestTimeOut());
        final RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                log.error("RestTemplate访问异常，hasError结果为：{}", response.getStatusText());
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                log.error("RestTemplate访问异常，handleError结果为：{}", response.getStatusText());
            }
        });
        return restTemplate;
    }

    @Bean
    public RestTemplate urlRestTemplate(RestTemplateConfig restTemplateConfig) {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(restTemplateConfig.getHttpConnectTimeOut());
        httpRequestFactory.setReadTimeout(restTemplateConfig.getHttpReadTimeOut());
        httpRequestFactory.setConnectionRequestTimeout(restTemplateConfig.getHttpConnectionRequestTimeOut());
        final RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                log.error("Http-RestTemplate访问异常，hasError结果为：{}", response.getStatusText());
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                log.error("Http-RestTemplate访问异常，handleError结果为：{}", response.getStatusText());
            }
        });
        return restTemplate;
    }

    @Bean
    @DependsOn({"urlRestTemplate", "restTemplate"})
    @ConditionalOnMissingBean(IHttpRestTemplateManager.class)
    public IHttpRestTemplateManager httpCommunicationManager(ApplicationContext applicationContext) {
        return new HttpRestTemplateBasic(applicationContext);
    }

    /**
     * 类描述: 构建RestTemplate原始基础对象
     *
     * @author : 王雷
     * @date : 2020/10/7 0007 下午 7:09
     */
    static class HttpRestTemplateBasic extends AbstractHttpRestTemplateManager {

        protected HttpRestTemplateBasic(ApplicationContext applicationContext) {
            super(applicationContext);
        }
    }
}
